/*
 * myPPlayer.cpp
 *
 *  Created on: Oct 31, 2011
 *      Author: administratorforpc
 */

#include "myPPlayer.h"
#include <MAUI/ListBox.h>
#include <MAUI/Image.h>
#include <madmath.h>
#include <mastdlib.h>
#include <ma.h>
#include <mavsprintf.h>

myPPlayer::myPPlayer(){
	giveHand();
	sortHand();
	for(int i = 0; i < 5; i++){
		isChecked[i] = false;
	}
	dupCheck();
	printHand();
}

void myPPlayer::printHand(){
	int hand;
	int suit;
	for(int i = 0; i < 5; i++){
		hand = currHand[i];
		lprintfln("HAND %hand", hand);
		suit = currSuit[i];
		lprintfln("SUIT %suit", suit);
	}
}

void myPPlayer::giveHand(){

	int randSuit = 0;
	int randCard = 0;
	srand(maTime());
	for(int i = 0; i < 5; i++)	// set cards to random values
	{
		randCard = rand() % 12;
		currHand[i] = randCard;
		randSuit = rand() % 3;
		currSuit[i] = randSuit;
	}
}

void myPPlayer::calcMyHand()
{
	if(isPair()){
		if(isTwoPair())
		{
			if(isThree())
			{
				if(isFour())
				{
					handWeight = 8 * (getWeights(4));
				}
				else if(isFullHouse())
				{
					handWeight = 7 * (getWeights(5));
				}
				else{
					handWeight = 4 * (getWeights(3));
				}
			}
			else{
				handWeight = 3 * (getWeights(2));
			}
		}
		else{
			handWeight = 2 * (getWeights(1));
		}
	}



	else if(isFlush())
	{
		if(isStraight())
		{
			handWeight = 9 * (getWeights(5));
		}
		else{
			handWeight = 6 * (getWeights(5));
		}
	}

	else if(isStraight())
	{
		handWeight = 5 * (getWeights(5));
	}

	else{
		handWeight = 0;
	}
}

int myPPlayer::myHand()
{
	return handWeight;
}

void myPPlayer::giveCard(int card)
{
	int mCard, mSuit, randCard, randSuit;

	bool done = false;

	while(!done){
		mCard = currHand[card];
		mSuit = currSuit[card];

		randCard = rand() % 12;
		randSuit = rand() % 3;

		if((randCard != mCard) && (randSuit != mSuit)){
			currHand[card] = randCard;
			currSuit[card] = randSuit;
			done = true;
		}
	}
	sortHand();
	dupCheck();
}

int myPPlayer::getWeights(int cards){

	int value = 0;

	if(cards == 1)
	{
		for(int i = 0; i < 5; i++)
		{
			for(int j = 0; j < 5; j++)
			{
				if(currHand[i] == currHand[j])
				{
					return (currHand[i] + currHand[j]);	// adds cards value together
				}
			}
		}
	}

	if(cards == 2){
		int first, second;

		for(int i = 0; i < 5; i++)
		{
			for(int j = 0; j < 5; j++)
			{
				if(currHand[i] == currHand[j])
				{
					first = i;
					second = j;
				}
			}
		}

		for(int x = 0; x < 5; x++)
		{
			for(int y = 0; y < 5; y++)
			{
				if((currHand[x] == currHand[y]) && (x != first) && (y != second))
				{
					return true;
				}
			}
		}
	}

	if(cards == 3)
	{
		for(int i = 0; i < 5; i++)
		{
			for(int j = 0; j < 5; j++)
			{
				if(currHand[i] == currHand[j])
				{
					return ((2 * currHand[i]) + currHand[j]);	// adds cards value together
				}
			}
		}
	}

	if(cards == 4)
	{
		for(int i = 0; i < 5; i++)
		{
			for(int j = 0; j < 5; j++)
			{
				if(currHand[i] == currHand[j])
				{
					return ((3 * currHand[i]) + currHand[j]);	// adds cards value together
				}
			}
		}
	}

	else
	{
		for(int i = 0; i < 5; i++)
		{
			value = currHand[i] + value;
		}
		return value;
	}
}

bool myPPlayer::isPair()
{
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if(currHand[i] == currHand[j])
			{
				return true;
			}
		}
	}

	return false;
}

bool myPPlayer::isTwoPair(){

	int first, second;

	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if(currHand[i] == currHand[j])
			{
				first = i;
				second = j;
			}
		}
	}

	for(int x = 0; x < 5; x++)
	{
		for(int y = 0; y < 5; y++)
		{
			if((currHand[x] == currHand[y]) && ((x != first) && (y != second)) && ((x != second) && (y != first)))
			{
				return true;
			}
		}
	}

	return false;
}

bool myPPlayer::isThree(){

	bool found = false;
	int checkCard = 0;
	int card1 = 0;
	int card2 = 0;

	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if(currHand[i] == currHand[j])
			{
				found = true;
				checkCard = i;
				card1 = i;
				card2 = j;
			}
		}
	}

	if(found == true){
		for(int x = 0; x < 5; x++){
			if((currHand[checkCard] == currHand[x]) && (card1 != x) && (card2 != x))
			{
				return true;
			}
		}
	}

	return false;
}

bool myPPlayer::isFour(){

	bool found2 = false;
	bool found3 = false;
	int checkCard = 0;
	int card1 = 0;
	int card2 = 0;
	int card3 = 0;

	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if(currHand[i] == currHand[j])
			{
				found2 = true;
				checkCard = i;
				card1 = i;
				card2 = j;
			}
		}
	}

	if(found2 == true){
		for(int x = 0; x < 5; x++){
			if((currHand[checkCard] == currHand[x]) && (card1 != x) && (card2 != x))
			{
				found3 = true;
				card3 = x;
			}
		}
	}

	if(found3 == true){
		for(int y = 0; y < 5; y++){
			if((currHand[checkCard] == currHand[y]) && (card1 != y) && (card2 != y) && (card3 != y)){
				return true;
			}
		}
	}

	return false;
}

bool myPPlayer::isFullHouse(){
	bool found2 = false;
	bool found3 = false;
	int checkCard = 0;
	int card1 = 0;
	int card2 = 0;
	int card3 = 0;

	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if(currHand[i] == currHand[j])
			{
				found2 = true;
				checkCard = i;
				card1 = i;
				card2 = j;
			}
		}
	}

	if(found2 == true){
		for(int x = 0; x < 5; x++){
			if((currHand[checkCard] == currHand[x]) && (card1 != x) && (card2 != x))
			{
				card3 = x;
				found3 = true;
			}
		}
	}

	if(found3 == true){
		for(int y = 0; y < 5; y++){
			for(int z = 0; z < 5; z++){
				if((currHand[y] == currHand[z]) && (y != card1) && ( y != card2) && ( y != card3) && (z != card1) && (z != card2) && (z != card3)){
					return true;
				}
			}
		}
	}


	return false;
}

bool myPPlayer::isFlush(){

	for(int i = 0; i < 4; i++)
	{
		if(currSuit[i] != currSuit[i+1])
		{
			return false;
		}
	}
	return true;
}

bool myPPlayer::isStraight(){
	bool isStraight = false;
	for(int j = 0; j < 4; j++){
		if((currHand[j] + 1) == currHand[j+1])
		{
			isStraight = true;
		}
		else{
			isStraight = false;
		}
	}

	return isStraight;
}

void myPPlayer::sortHand(){

	int num = 0;
	int tempSuit = 0;
	bool done = false;

	while(!done){
		done = true;
		for(int i = 0; i < 4; i++){

			if(currHand[i] > currHand[i+1])
			{
				num = currHand[i];
				tempSuit = currSuit[i];
				currHand[i] = currHand[i+1];
				currSuit[i] = currSuit[i+1];
				currHand[i+1] = num;
				currSuit[i+1] = tempSuit;

				done = false;
			}
		}
	}
}

void myPPlayer::playerClicked(int i){
	if(isChecked[i] == true)
		isChecked[i] = false;

	if(isChecked[i] == false)
		isChecked[i] = true;
}


bool myPPlayer::isClicked(int i){
	return isChecked[i];
}

int myPPlayer::getCard(int i){
	return currHand[i];
}

int myPPlayer::getSuit(int i){
	return currSuit[i];
}


void myPPlayer::dupCheck()
{
	int num = 0;
	int tempSuit = 0;
	bool done = false;
	while(!done){
		done = true;
		for(int i = 0; i < 4; i++){
			if((currHand[i] == currHand[i+1]) && (currSuit[i] == currSuit[i+1]))
			{
				currHand[i] = rand() % 12;
				currSuit[i] = rand() % 3;
				done = false;
			}
		}
	}
}
